package com.lonty.vote.util;

import com.lonty.vote.request.VoteList;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 陈璇
 * @Date 2020/5/26 12:05
 * @Version 1.0
 */
public class ExcelUtil {

    public static void exportExcel(HttpServletResponse response, List<VoteList> excelData, String sheetName, String fileName,
                                   int columnWidth) throws IOException {

        //声明一个工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //生成一个表格，设置表格名称
        HSSFSheet sheet = workbook.createSheet(sheetName);
        //创建表头
        createTitle(workbook, sheet);
        //设置为居中加粗

        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        //写入List<VoteList>中的数据
        for(int i = 0;i < excelData.size();i++){
            //创建行
            HSSFRow row = sheet.createRow(i + 1);
            //设置行高
            row.setHeightInPoints(20);
            VoteList voteList = excelData.get(i);
            //第一个单元格
            //创建一个单元格
            HSSFCell firstCell = row.createCell(0);
            //创建一个内容对象
            HSSFRichTextString firstText = new HSSFRichTextString(voteList.getName());
            //将内容对象的文字内容写入到单元格中
            firstCell.setCellValue(firstText);
            List<Integer> nums = voteList.getNums();
            for(int j = 0;j < nums.size();j++){
                //创建一个单元格
                HSSFCell cell = row.createCell(j + 1);
                //创建一个内容对象
                HSSFRichTextString text = new HSSFRichTextString(nums.get(j).toString());
                //将内容对象的文字内容写入到单元格中
                cell.setCellValue(text);
                cell.setCellStyle(style);
            }
        }

        //准备将Excel的输出流通过response输出到页面下载
        //八进制输出流
        response.setContentType("application/octet-stream");

        //设置导出Excel的名称
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8") + ".xls");

        //刷新缓冲
        response.flushBuffer();

        //workbook将Excel写入到response的输出流中，供页面下载该Excel文件
        workbook.write(response.getOutputStream());

        //关闭workbook
        workbook.close();
    }

    //创建表头
    private static void createTitle(HSSFWorkbook workbook,HSSFSheet sheet){
        HSSFRow row = sheet.createRow(0);
        //设置行高
        row.setHeightInPoints(20);
        //设置表格列宽度
        sheet.setDefaultColumnWidth(30);
        //设置为居中加粗
        HSSFCellStyle style = workbook.createCellStyle();
        HSSFFont font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        //写入List<List<String>>中的数据
        List<String> head = new ArrayList<>();
        head.add("名称-日期");
        head.add("06-01");
        head.add("06-02");
        head.add("06-03");
        head.add("06-04");
        head.add("06-05");
        head.add("06-06");
        head.add("06-07");
        head.add("总票数");
        for(int i = 0;i < head.size();i++){
            //创建一个单元格
            HSSFCell cell = row.createCell(i);
            //创建一个内容对象
            HSSFRichTextString text = new HSSFRichTextString(head.get(i));
            //将内容对象的文字内容写入到单元格中
            cell.setCellValue(text);
            cell.setCellStyle(style);
        }
    }

}
